﻿using System.Windows;
using System.Windows.Input;
using HeritageAcademy.Models.ViewModels;

namespace HeritageAcademy.Window
{
	/// <summary>
	/// Window frame class. 
	/// Draging and main window control buttons (max, min, close) are implemented here.
	/// </summary>
	public partial class WindowFrame
    {
        #region Ctor
        
        public WindowFrame()
        {
            InitializeComponent();
            Loaded += WindowFrame_Loaded;
        }

        #endregion

        #region Event Handlers
        
        /// <summary>
        /// Register drag window handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void WindowFrame_Loaded(object sender, RoutedEventArgs e)
        {
            dragTarget.MouseLeftButtonDown += dragTarget_MouseLeftButtonDown;
            MouseDoubleClick += WindowFrame_MouseDoubleClick;
        }

        /// <summary>
        /// Window movement handler.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Mouse button event arguments</param>
        static void dragTarget_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Application.Current.MainWindow.DragMove();    
        }

        /// <summary>
        /// Window state change event handler.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Mouse button event arguments</param>
        void WindowFrame_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.GetPosition(this).Y >= 80.0) return;
            switch (Application.Current.MainWindow.WindowState)
            {
                case WindowState.Maximized:
                    Application.Current.MainWindow.WindowState = WindowState.Normal;
                    break;
                case WindowState.Normal:
                    Application.Current.MainWindow.WindowState = WindowState.Maximized;
                    break;
            }
        }

	    /// <summary>
        /// Minimize window button click handler.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Routed event arguments</param>
        void minimize_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.MainWindow.WindowState = WindowState.Minimized;
        }

        /// <summary>
        /// Maximize window button click handler.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Routed event arguments</param>
        void maximize_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.MainWindow.WindowState = WindowState.Maximized;
        }

        /// <summary>
        /// Restore window button click handler.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Routed event arguments</param>
        void restore_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.MainWindow.WindowState = WindowState.Normal;
        }

        /// <summary>
        /// Close window button click handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void close_Click(object sender, RoutedEventArgs e)
        {
            // Set all users status flag to "false" (closed from ReadingView)
            var udata = new UsersDataHandler();
            udata.UnSetCurrentUser();
            // Close app
            Application.Current.MainWindow.Close();
        }

        #endregion
    }
}